# determine which covariable should be removed
# 
# Author: guochun
###############################################################################


variableSelection=function(fittedModel,alpha=0.05){
	pars=fittedModel@parameters
	np=length(pars)
	pvalue=numeric()
	for (i in 1:np){
		pvalue=c(pvalue,pars[[i]]@pvalue)
	}
	insig=pvalue>alpha
	#check is there any pvalue larger than alpha
	del=NA
	if(fittedModel@select & any(insig)){
		del=which(pvalue==max(pvalue,na.rm=T))[1]
	}
	#browser()
	attr(del,"pvalues")=pvalue
	return(del)
}


multiVariableSelection=function(fittedModel,alpha=0.05){
	pars=fittedModel@parameters
	np=length(pars)
	pvalue=numeric()
	for (i in 1:np){
		pvalue=c(pvalue,pars[[i]]@pvalue)
	}
	insig=pvalue>alpha
	#check is there any pvalue larger than alpha
	del=NA
	if(fittedModel@select & any(insig)){
		del=which(insig)
	}
	result=list()
	k=0
	if(length(del)>1){
		for(i in 1:length(del)){
			tp=combn(del,i)
			for(j in 1:dim(tp)[2]){
				k=k+1
				result[[k]]=as.vector(tp[,j])
			}
		}
	}
	if(length(result)==0)
	    result=list(NA)
	#browser()
	attr(result,"pvalues")=pvalue
	return(result)
}